﻿using Abp.Application.Services.Dto;
using Abp.AspNetCore.Mvc.Controllers;
using Batman.Cms.Core.Core.Attributes;
using Batman.Cms.Services.Applications;
using Batman.Cms.Services.Applications.Filters;
using Batman.Cms.Services.Applications.Inputs;
using Microsoft.AspNetCore.Mvc;

namespace Batman.Cms.Controllers.Applications
{
    public class ApplicationDataController : AbpController
    {
        public ApplicationDataReader applicationDataReader { get; set; }

        public ApplicationDataManager applicationDataManager { get; set; }

        /// <summary>
        /// 获取应用数据
        /// </summary>
        /// <param name="applicationId">应用编号</param>
        /// <param name="applicationDataId">应用数据编号</param>
        /// <returns></returns>
        public IActionResult Get(int applicationId, int applicationDataId)
        {
            var result = applicationDataReader.Get(applicationId, applicationDataId);

            // 设置Parent对象为空。否则会导致序列化对象出现循环问题。
            result.Children.ForEach(t => t.Parent = null);

            return Json(result);
        }

        [ActionName("get-list")]
        [AcceessOriginal]
        public IActionResult Get(PagedResultRequestDto page, ApplicationDataFilter filter)
        {
            var result = applicationDataReader.Get(page, filter);
            return Json(result);
        }

        [AcceessOriginal]
        public IActionResult Create(CreateApplicationDataInput model)
        {
            var result = applicationDataManager.Create(model);
            return Json(result);
        }
    }
}
